#include <algorithm>
#include <cstdio>

const int N = 1005;
int n, f[N][N];

struct Node {
  int l, d;
} a[N];

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("testdata.in", "r", stdin);
  freopen("testdata.out", "w", stdout);
#else
  freopen("specialist.in", "r", stdin);
  freopen("specialist.out", "w", stdout);
#endif
#endif

  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) { scanf("%d", &a[i].l); }
  for (int i = 1; i <= n; ++i) { scanf("%d", &a[i].d); }
  for (int i = 1; i <= n; ++i) {
    for (int j = 0; j <= n; ++j) {
      f[i][j] = f[i - 1][j];
      if (j >= a[i].l) f[i][j] = std::max(f[i][j], f[i - 1][j - a[i].l] + a[i].d);
    }
  }
  int ans = 0;
  for (int i = 0; i <= n; ++i) ans = std::max(ans, f[n][i]);
  printf("%d", ans);
  return 0;
}